//
//  VisitorView.swift
//  swft_weibo
//
//  Created by mac on 15/9/11.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

protocol visitorViewDelegate : NSObjectProtocol{

    //登入回调
    func loginBtnWillClick()
    //注册回调
    func registerBtnWillClick()

}

class VisitorView: UIView {
    
    weak var delegate : visitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件到自身中去
        addAllChildrenComponent()
        
        //设置所有的子控件的布局
        addLayoutAllChildrenComponet()
        
        //设置visitor背景颜色为白色
        backgroundColor=UIColor.whiteColor()
        
        //开起转盘动画效果
        startDynamicAnnimation()
        
        
    }
    /**
    修改显示末登入状况下的图片内容
    
    :param: iconview  <#iconview description#>
    :param: titleText <#titleText description#>
    */
     func convertShowIcons(iconview:String?,titleText:String?){

        self.iconView?.image=UIImage(named: iconview!)
        print(iconView)
        instructionLable?.text=titleText
    
        dynamicView?.hidden=true
    }
    
    /**
    添加子控件到自身中去
    */
    private func addAllChildrenComponent()
    {
        
        addSubview(dynamicView!)
        addSubview(bgMask!)
        addSubview(registerBtn!)
        addSubview(loginBtn!)
        addSubview(instructionLable!)
        addSubview(iconView!)
    }
    /**
    给所有子控件进行布局操作
    */
    private func addLayoutAllChildrenComponet()
    {
        iconView?.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: CGSizeMake(100, 100))
        
        dynamicView?.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        //注意在布局label的宽度的时候，我们用了最原始的NSLayoutConstraint进行操作，为什么了？因为如果我用下面的size参数进行布局label的size话，就有可能出现问题。因为size是长，宽都会一起进行布局的。但是我只想要布局lable的宽度限制这样的话就只能用下面的方法了，因此我的用的这个布局分类并没有提供布局一个控件的长的方法，因此我就用最原始的了。
        instructionLable?.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: dynamicView!, size: nil, offset: CGPointMake(0, 5))
        
        let lableWidth = NSLayoutConstraint(item: instructionLable!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
        addConstraint(lableWidth)
        //
        registerBtn?.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView:instructionLable!, size: CGSizeMake(80, 30), offset: CGPointMake(0, 10))
        
        loginBtn?.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: instructionLable!, size: CGSizeMake(80, 30), offset: CGPointMake(0, 10))
        
        bgMask?.xmg_Fill(self)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /**
    开始转盘的动画效果
    */
    func startDynamicAnnimation(){
        let basicAnnimation=CABasicAnimation(keyPath: "transform.rotation")
        
        basicAnnimation.toValue=M_PI*2
        basicAnnimation.duration=20
        basicAnnimation.repeatCount=MAXFLOAT
        basicAnnimation.removedOnCompletion=false
        
        dynamicView?.layer.addAnimation(basicAnnimation, forKey: nil)

    }
    /**
    设置登入的点击事件
    */
    func loginBtnClick(){
    
        delegate?.loginBtnWillClick()
    
    }
    /**
    设置注册的点击事件
    */
    func registerBtnClick(){
    
        delegate?.registerBtnWillClick()
    }
    
    /// icon小房子
    lazy var iconView :UIImageView? = {
        let smallIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return smallIcon
    }()
    /// 小转盘转动
    lazy var dynamicView :UIImageView? = {
    
        let smallDynamic = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return smallDynamic
    }()
    /// 说明性label
    lazy var instructionLable :UILabel? = {
    
        let smallLable  = UILabel()
        smallLable.text="关注一些人，回这里看看有什么惊喜"
        smallLable.numberOfLines=0
        smallLable.textColor=UIColor.grayColor()
        return smallLable
    
    }()
    /// 注册按钮
    lazy var registerBtn :UIButton? = {
        let smallRegisterBtn = UIButton()
        smallRegisterBtn.setTitle("注册", forState: UIControlState.Normal)
        smallRegisterBtn.sizeToFit()
//        smallRegisterBtn.backgroundColor=UIColor.redColor()
        smallRegisterBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        smallRegisterBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置按钮注册点击事件
        smallRegisterBtn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return smallRegisterBtn
    }()
    /// 登入按钮
    lazy var loginBtn :UIButton? = {
        let smallLoginBtn=UIButton()
        smallLoginBtn.setTitle("登入", forState: UIControlState.Normal)
        smallLoginBtn.sizeToFit()
//        smallLoginBtn.backgroundColor=UIColor.redColor()
        smallLoginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        smallLoginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置按钮登入点击事件
        smallLoginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return smallLoginBtn
    
    }()
    /// 设置背景图片
    lazy var bgMask :UIImageView? = {
        let smallBGMask=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return smallBGMask
    
    }()
    
}
